import HttpUtils from '../HttpUtils/HttpUtils';//引入二维码js
const QR = require("../../utils/wxqrcode.js");
// var sssss = require("../../utils/base64src.js");

// 获取当前时间戳
var timestamp = Date.parse(new Date()) / 1000;
Page({
	...HttpUtils.func,
	data: {
		titOn: 'all',
		pageIndex: 1,
		pageItems: 10,
		totalPages: 0,
		status: '',
		showQrCode: false,
		partyMeetList: [],

		// 二维码变量
		qrImagePath: '',
		qrTitle: '',
		qrStartTime: '',
		height: '',
		flag: false,
	},
	handleTap1(event) {   //会议筛选
		var sessType = event.target.id;
		var going;
		var pass;
		var absence;
		if (sessType == "going") {
			going = "goingShow";
			pass = "passHide";
			absence = "absenceHide";
		} else if (sessType == "pass") {
			pass = "passShow";
			going = "goingHide";
			absence = "absenceHide";
		} else if (sessType == "absence") {
			absence = "absenceShow";
			going = "goingHide";
			pass = "passHide";
		} else if (sessType == "all") {
			absence = "absenceShow";
			going = "goingShow";
			pass = "passShow";
		}
		this.setData({
			titOn: sessType,
			going: going,
			pass: pass,
			absence: absence
		})
	},

	onReady() {
	},

	onLoad() {
		//支部会议查询列表接口
		this.loadpartyMeetList(this.data.status);
	},

	//查询我的 会议任务列表
	loadpartyMeetList: function(e) {
		var status = 0;
		if ("string" == typeof (e)) {
			status = e;
		} else {
			if (e != undefined) {
				status = e.currentTarget.dataset.code;  //表示从页面上点击过来de,是一个对象
				this.handleTap1(e)
				this.setData({
					"pageIndex": 1,
					"status": status
				})
			} else {
				status = this.data.status;    //表示从页面上拉加载的 直接获取
			}
		}
		var newUrl = getApp().globalData.dangjianUrl + "/party/partyMeetList";
		var params = {};
		params["pageIndex"] = this.data.pageIndex + "";
		params["pageItems"] = this.data.pageItems + "";
		params["status"] = status + "";
		params["meetType"] = "";
		params["test"] = "";
		params = JSON.stringify(params);

		this.httpGet(newUrl, params, "支部会议查询列表接口调用失败").then(
			(respData) => {
				console.log(respData)
				var result = respData.data.result.code;
				if (1 == result) {
					var meets = respData.data.meets;
					var totalPages = meets.totalPages;
					var pageIndex = meets.pageIndex;
					var items = meets.items;
					var className;
					var statusName;
					meets = meets.meet;

					var partyMeetList = [];
					for (var index in meets) {
						var meet = meets[index];
						var json = {};
						var DateTime = getApp().formatToDate(meet.startDate, meet.endDate);
						json["creatorId"] = meet.creatorId;
						json["userId"] = getApp().globalData.userId;
						json["address"] = meet.address;
						json["startDate"] = meet.startDate;
						json["endDate"] = meet.endDate;
						json["DateTime"] = DateTime;
						json["meetId"] = meet.meetId;
						json["sign"] = meet.sign;
						json["status"] = meet.status;
						// 会议状态 1表示未开始2表示进行中3表示已结束 
						// 签到状态 1=未报名2=已报名3=已报名未签到4=已报名已签到5=请假6=缺勤
						json["className"] = className;
						json["statusName"] = statusName;
						json["subject"] = meet.subject;
						json["title"] = meet.title;
						json["sign"] = meet.sign;
						partyMeetList.push(json);
					}
					console.log(partyMeetList)
					if (e == undefined) {
						var oldpartyMeetList = this.data.partyMeetList;
						partyMeetList = oldpartyMeetList.concat(partyMeetList);          //表示从页面上拉加载的 直接获取
					}
					this.setData({
						"partyMeetList": partyMeetList,
						"totalPages": totalPages,
						"pageIndex": pageIndex,
						"pageitems": items,
					});
        } else {
          this.showPageMsg("alert", respData.data.result.description)
				}
			}
		);
	},

	//会议详情
	meetingDetail(event) {
		var meetId = event.target.dataset.meetid;
		var status = event.target.dataset.status;
		var sign = event.target.dataset.sign;
		var pageUrl = '../meetingDetail/meetingDetail?meetId=' + meetId + '&status=' + status + '&sign=' + sign;
		wx.navigateTo({
			url: pageUrl
		})
	},

	// 页面被拉到底部
	onReachBottom: function(e) {
		var pageIndex = this.data.pageIndex + 1;
		var totalPages = this.data.totalPages;
		var status = this.data.status;
		if (totalPages >= pageIndex) {
			this.setData({
				"pageIndex": pageIndex,
				"status": status,
			});
			this.loadpartyMeetList();
		} else {
			this.showPageMsg("tips", "没有更多内容了");
		}
	},

	onPageScroll: function(e) { // 获取滚动条当前位置
		// console.log(e)
		// console.log(e.scrollTop)//获取滚动条当前位置的值
		// this.setData({
		// 	height: e.scrollTop - 667,
		// })
	},

	aa() {
		var flag = this.data.flag;
		if (this.data.showQrCode) {
			this.setData({
				flag: true
			})
		} 
	},

	// 页面 上拉刷新
	onPullDownRefresh: function(e) {
		this.setData({
			pageIndex: 1,
			contentList: [],
		});

		this.onLoad();
		wx.stopPullDownRefresh();
	},


	//跳转二维码
	qrCodeDetail(event) {
		var startDate = event.currentTarget.dataset.startDate;
		var meetId = event.currentTarget.dataset.meetId;
		var title = event.currentTarget.dataset.title;
		var qrStartTime = startDate.substring(startDate.indexOf(':') - 2, startDate.indexOf(':') + 3);
		let that = this, text;

		if (event.target.tagName == 'image') {
			that.setData({
				showQrCode: !this.data.showQrCode,
			})

			try {
				text = 'meetId=' + meetId;
				let _img = QR.createQrCodeImg(text, {
					size: parseInt(200)
				})
				that.setData({
					'qrImagePath': _img,
					'qrTitle': title,
					'qrStartTime': qrStartTime,
				})
			} catch (event) {
				console.log(event)
			}
		}
	},
	// 验证
	goScan() {
		wx.navigateTo({
			url: '../scanForMeeting/scanForMeeting'
		})
	},
	//关闭
	closePop() {
		let that = this;
		that.setData({
			showQrCode: !this.data.showQrCode,
		})
	},
	// 二维码长按保存，暂未实现
	longTapImg(e) {
		var url = e.currentTarget.dataset.url;
		wx.downloadFile({
			url: 'http://img.alicdn.com/tfs/TB1x669SXXXXXbdaFXXXXXXXXXX-520-280.jpg',
			success({ filePath }) {
				wx.saveImage({
					url: filePath,
					success(res) {
            this.showPageMsg("tips", "保存成功")  
					},
				});
			},
			fail(res) {
        this.showPageMsg("tips", res.errorMessage || res.error)  
			},
		});
	},
});
